## -*- mode: html; coding: utf-8; -*-
## This file is part of CDS Invenio.
## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 CERN.
##
## CDS Invenio is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## CDS Invenio is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with CDS Invenio; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.


<!-- WebDoc-Page-Title: BibEdit Internals -->
<!-- WebDoc-Page-Navtrail: <a class="navtrail" href="<CFG_SITE_URL>/help/hacking">Hacking CDS Invenio</a> -->
<!-- WebDoc-Page-Navbar-Select: hacking-bibedit-internals -->

<p>BibEdit.BibRecord library offer a whole set of API function to handle record metadata.</p>

<h2>Managing metadata with BibEdit.BibRecord</h2>
<p>In order to work with bibrecord library you first need to have available a record representation.</p>

<p>If you have a MARCXML representation of the record to be handled, you can use the <tt>create_record</tt> function to obtain a bibrecord internal representation:
<pre>
    from invenio.bibrecord import create_record
    record = create_record(marcxml)[0]
</pre>
</p>

<p>If you want to handle a record stored in the system and you know the record ID, then you can easily exploit CDS Invenio search_engine API to obtain the corresponding marcxml:
<pre>
    from invenio.bibrecord import create_record
    from invenio.search_engine import print_record
    marcxml = print_record(rec_id, 'xm')
    record = create_record(marcxml)[0]
</pre>
</p>

<p>Having an internal representation of a record you can manipulate it by means of bibrecord functions like <tt>record_get_field_instances</tt>, <tt>record_has_field</tt>, <tt>record_add_field</tt>, <tt>record_delete_field</tt>, <tt>record_delete_subfield</tt>, <tt>record_add_or_modify_subfield</tt>, record_add_subfield, <tt>record_does_field_exist</tt>, <tt>record_filter_fields</tt>, <tt>record_replace_in_subfields</tt>, <tt>record_get_field_value</tt>, <tt>record_get_field_values</tt>...</p>

<p>At the end, if you want the MARCXML representation of the record you can use <tt>record_xml_output</tt>:
<pre>
    from invenio.bibrecord import create_record
    from invenio.search_engine import print_record
    marcxml = print_record(rec_id, 'xm')
    record = create_record(marcxml)[0]
    ... manipulation ...
    new_marcxml = record_xml_output(record)
</pre>
</p>

<p>In order to write back such a record into the system you should use the BibUpload utility.</p>

<p>Please referer to bibrecord.py for a complete and up-to-date description of the API.</p>

<p>As always, a good entry point to the bibrecord library and its record structure manipulating functions 
is to read the unit test cases that are located in <code>bibrecord_tests.py</code> and <code>bibupload_regression_tests.py</code>.</p>
